package com.cgy.utils.netx.ftp.client;

import com.cgy.utils.netx.client.INetworkClientLibrary;
import com.cgy.utils.netx.config.ClientConfig;
import com.cgy.utils.netx.ftp.utils.Ftps;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;

/**
 * @author CHTK
 */
public class FtpClient implements INetworkClientLibrary<Ftps> {

    private Ftps ftpClient;

    @Override
    public INetworkClientLibrary<Ftps> initial(ClientConfig clientConfig, Object...extConfig) {
        Preconditions.checkArgument(null != clientConfig);
        Preconditions.checkArgument(StringUtils.isNotBlank(clientConfig.getUsername()));
        Preconditions.checkArgument(StringUtils.isNotBlank(clientConfig.getPassword()));
        Preconditions.checkArgument(StringUtils.isNotBlank(clientConfig.getIp()));
        Preconditions.checkArgument(clientConfig.getPort() > 0);
        this.ftpClient = Ftps.getFTPClient(clientConfig.getIp(), clientConfig.getUsername(), clientConfig.getPassword(), clientConfig.getPort());
        return this;
    }

    @Override
    public INetworkClientLibrary initial(InputStream inputStream) {
        return this;
    }

    @Override
    public INetworkClientLibrary<Ftps> connect() throws Exception {
        return this;
    }

    @Override
    public void release() throws Exception {
        this.ftpClient.release();
    }

    @Override
    public Ftps getClient() throws Exception {
        return ftpClient;
    }
}
